//
//  SecurityStatisticsVC.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/5/19.
//
// 【首页-保安管理】总行、分行、支行看数据

import Foundation
import UIKit

class SecurityStatisticsVC: SubLevelViewController {
    let sticsV = SecurityStatisticsView()
    
    var param: SecurityParam = SecurityParam()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .bg
        navigationController?.navBarStyle(.white)
        if deptName == nil {
            deptName = BSUser.currentUser.deptName
            deptId = BSUser.currentUser.deptId
        }
        
        title = deptName ?? "保安管理"
        param.deptId = deptId
        param.dateType = dateEnum.rawValue
        param.date = date
        
        setupUI()
        
        reloadData()
    }
    
    func reloadData() {
        sticsV.reloadData(deptName: deptName, date: date, type: dateEnum)
        fetchData()
    }
    
    func fetchData() {
        view.showToastActivity()

        var index = 0
        // 履职情况
        API.getSecurityScaleByDept(withParam: param) { responseModel in
            index += 1
            if responseModel.errorCode == .Success {
                DispatchQueue.main.async {
                    self.sticsV.reloadData(balzModel: responseModel.model)
                    if index == 2 {
                        self.view.hideToastActivity()
                    }
                }
            }
        }
        
        // 在岗情况
        API.getBaglByDept(withParam: param) { responseModel in
            index += 1
            if responseModel.errorCode == .Success {
                DispatchQueue.main.async {
                    self.sticsV.reloadData(baglModel: responseModel.model)
                    if index == 2 {
                        self.view.hideToastActivity()
                    }
                }
            }
        }
        
        API.getLzglWdwcqk(withParam: LzglBaseParam(dateType: param.dateType, deptId: param.deptId, date: param.date)) { responseModel in
            DispatchQueue.main.async {
                if let m = responseModel.model {
                    self.sticsV.update(withWdwcqk: m)
                }
            }
        }
    }
    
    func setupUI() {
        sticsV.baseTV.title = "保安管理"
        sticsV.sDelegate = self
        sticsV.customV.delegate = self
        sticsV.deptBtn.delegate = self
        view.addSubview(sticsV)
        sticsV.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
        }
    }
}

extension SecurityStatisticsVC: SecurityStatisticsViewDelegate {
    func handleLzqkSelected(_ modal: TaskDeptRateModal) {
        if modal.sfczzj == true {
            let subVC = SecurityStatisticsVC(withDeptId: modal.wddm, deptName: modal.wdmc, date: date, dateEnum: dateEnum)
            navigationController?.pushViewController(subVC, animated: true)
        } else {
            let subVC = SecurityStatisticsTopVC(withDeptId: modal.wddm, deptName: modal.wdmc, date: date, dateEnum: dateEnum)
            navigationController?.pushViewController(subVC, animated: true)
        }
    }
    
    func handleZgqkSelected(_ modal: BaglByDeptWdModel) {
        if modal.sfczzj == true {
            let subVC = SecurityStatisticsVC(withDeptId: Int64(modal.wddm ?? "-1"), deptName: modal.wdmc, date: date, dateEnum: dateEnum)
            navigationController?.pushViewController(subVC, animated: true)
        } else {
            let subVC = SecurityStatisticsTopVC(withDeptId: Int64(modal.wddm ?? "-1"), deptName: modal.wdmc, date: date, dateEnum: dateEnum)
            navigationController?.pushViewController(subVC, animated: true)
        }
    }
}

extension SecurityStatisticsVC: BSCustomDateViewDelegate {
    func didSelected(year: Int, quarter: Int?, month: Int?, day: Int?, dateEnum: CustomDateEnum) {
        let m = month ?? 1
        let d = day ?? 1
        self.date = "\(year)-\(m < 10 ? "0\(m)" : "\(m)")-\(d < 10 ? "0\(d)" : "\(d)")"
        self.dateEnum = dateEnum

        param.dateType = dateEnum.rawValue
        param.date = self.date
        
        fetchData()
    }
}

extension SecurityStatisticsVC: BSDeptStiscControlDelegate {
    func handleSelected(_ dept: TaskDeptRateModal?) {
        if dept?.sfczzj == true {
            let subVC = SecurityStatisticsVC(withDeptId: dept?.wddm, deptName: dept?.wdmc, date: date, dateEnum: dateEnum)
            navigationController?.pushViewController(subVC, animated: true)
        } else {
            let vc = SecurityStatisticsTopVC(withDeptId: dept?.wddm, deptName: dept?.wdmc, date: date, dateEnum: dateEnum)
            navigationController?.pushViewController(vc, animated: true)
        }
    }
}
